﻿using System;
using System.Collections.Generic;
 
using System.Text;
using System.Net.Sockets;
using System.IO;

namespace GGSharp.Packets
{
    public class HeaderPacket : Structures.StuctureBase
    {
        private PacketType _type;
        private bool _autoReadToEnd = false;

        protected bool AutoReadToEnd
        {
            get { return _autoReadToEnd; }
            set { _autoReadToEnd = value; }
        }

        public PacketType Type
        {
            get { return _type; }
            set { _type = value; }
        }

        public void Read(Stream stream)
        {
            Read(new BinaryReader(stream));
        }

        public override void Read(BinaryReader reader)
        {
            //_type = (PacketType)reader.ReadInt32();
            Length = reader.ReadInt32();

            if (AutoReadToEnd)
                ReadToEnd(0, reader);
        }

        protected void ReadToEnd(int startIndex, BinaryReader reader)
        {
            ReadedLength += reader.ReadBytes(this.Length - startIndex).Length;
        }

        public  void Write(Stream stream)
        {
            Write(new BinaryWriter(stream));
        }

        public override void Write(BinaryWriter writer)
        {
            writer.Write((int)_type);
            writer.Write(Length);
        }
    }
}
